
package com.bbh.plugin.single.privilege.demo.controller.mail;

import com.bbh.plugin.single.privilege.core.entity.MailTemplate;
import com.bbh.plugin.single.privilege.core.service.MailTemplateService;
import com.bbh.plugin.single.privilege.demo.controller.BasicController;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller - 邮件管理
 *
 * @author ZHANGTYONG
 * @version 3.0
 */
@Controller
@RequestMapping("/mail/template")
public class MailTemplateController extends BasicController {
    @Autowired
    private MailTemplateService mailTemplateService;


    /**
     * 跳转到邮件模板管理页面
     */
    @RequestMapping(value = VIEW_FILED + "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
//        model.addAttribute("mailTypeItems", EnumSet.allOf(MailEnum.MAIL_TYPE.class));
        return "single/mail/mailTemplate";
    }


    /**
     * 邮件模板列表
     *
     * @return
     * @throws
     */
    @RequestMapping(value = VIEW_FILED + "/list")
    @ResponseBody
    public GridVo list(HttpServletRequest request, String appKey, String name) throws Exception {
        Condition params = new Condition();
        if (StringUtils.isNotBlank(appKey)) {
            params.putEquals("appKey", appKey);
        }
        if (StringUtils.isNotBlank(name)) {
            params.put("name", Condition.LIKE, name);
        }
        GridVo vo = new GridVo();
        Page<MailTemplate> pageResult = mailTemplateService.selectPage(params, null, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));
        return new GridVo(pageResult);
    }

    /**
     * 跳转到模板 edit页面
     *
     * @return
     */
    @RequestMapping(INSERT_FILED + "/add")
    public String add(Model model) {
        return "single/mail/mailTemplateAdd";
    }

}